package com.smartao.base.employee;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.smartao.base.department.Department;
import com.smartao.base.department.DepartmentService;
import com.smartao.base.entityselector.Selectable;
import com.smartao.core.mvc.BaseAction;
import com.smartao.core.orm.Page;
import com.smartao.core.orm.PageRequest;

@Controller
@Scope("prototype")
@Results({ @Result(name = "json", type="json", params={"root","departments"}), 
		@Result(name = "jsonSuccess", type="json", params={"root","jsonMap"}),
		@Result(name = "jsonFailure", type="json", params={"root","jsonMap"}),
		@Result(name = "gotoList", type="redirect", location="employee.action"),
		@Result(name = "updateAccount", location="account.jsp"),
		@Result(name = "selector", location="employee-selector.jsp"),
		@Result(name = "update", location="update.jsp")})
public class EmployeeAction extends BaseAction implements Selectable{
	private static final long serialVersionUID = -472240074632626057L;
	
	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private EmployeeService employeeService;
	
	private Employee employee;
	
	private Page<Employee> page;
	
	private Long positionId;
	
	private Long departmentId;
	
	private String message;
	
	@Override
	public String execute(){
		PageRequest pageRequest = new PageRequest(pageNumber,pageSize);
		page = employeeService.pageQuery(employee,pageRequest);
		return SUCCESS;
	}
	
	public String preUpdate(){
		// 更新
		if(id!=null){
			employee = employeeService.findOne(id);
		}
		// 新增
		else{
			employee = new Employee();
			if (departmentId != null) {
				Department department = departmentService.findOne(departmentId);
				employee.setDepartment(department);
			}
		}
		return "update";
	}
	
	public String update(){
		if(employee.getId()!=null){
			Employee e = employeeService.findOne(employee.getId());
			employee.setRoles(e.getRoles());
			employee.setPositions(e.getPositions());
		}
		employeeService.save(employee);
		message = "保存成功.";
		return "update";
	}
	
	public String preUpdateAccount(){
		employee = employeeService.findOne(id);
		return "updateAccount";
	}
	
	public String updateAccount(){
		employeeService.updateAccount(
			employee.getId(),
			employee.getAccount(),
			employee.getPassword()
		);
		message = "保存成功.";
		return "updateAccount";
	}
	
	public String delete(){
		employeeService.delete(id);
		return "gotoList";
	}
	
	@Override
	//人员选择
	public String queryForSelect() {
		//TODO 多选的未处理
		PageRequest pageRequest = new PageRequest(pageNumber,pageSize);
		page = employeeService.pageQuery(employee,pageRequest);
		return "selector";
	}
	

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public Long getPositionId() {
		return positionId;
	}

	public void setPositionId(Long positionId) {
		this.positionId = positionId;
	}

	public Long getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Long departmentId) {
		this.departmentId = departmentId;
	}

	public String getMessage() {
		return message;
	}

	public Page<Employee> getPage() {
		return page;
	}

}
